<template>
  <div class="koukuan">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="部门" prop="departmentId">
        <el-select v-model="ruleForm.departmentId" placeholder="部门" @change="changDept">
          <el-option v-for="item in dlist.lists" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>

    <el-table
      ref="tableRef"
      :data="companyList"
      style="width: 100%"
      :header-cell-style="{background:'#eef1f6'}"
    >
      <el-table-column>
        <template slot-scope="scope">
          <div>
            <el-row :gutter="20">
              <el-col :span="6">
                <el-switch
                  v-model="scope.row.isEnable"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  :active-value="1"
                  :inactive-value="0"
                  :inactive-text="getTitle(scope.row.dedTypeCode)+'扣款'"
                />
              </el-col>
            </el-row>
            <el-row v-if="scope.$index !== 2" :gutter="20">
              <el-col :span="12" :offset="2">
                {{ getTitle(scope.row.dedTypeCode) }}≤
                <el-input v-model="scope.row.periodUpperLimit" placeholder="请输入内容" style="width: 50px;" />
                分钟
              </el-col>
            </el-row>
            <el-row v-if="scope.$index === 2" :gutter="20">
              <el-col :span="12" :offset="2">
                {{ getTitle(scope.row.dedTypeCode) }}按
                <el-input v-model="scope.row.fineSalaryMultiples" placeholder="请输入内容" style="width: 50px;" />
                倍工资处罚
              </el-col>
            </el-row>
            <el-row v-if="scope.$index !== 2" :gutter="20">
              <el-col :span="12" :offset="4">
                {{ getTitle(scope.row.dedTypeCode) }}≤
                <el-input v-model="scope.row.timesUpperLimit" placeholder="请输入内容" style="width: 50px;" />
                次，每次扣款
                <el-input v-model="scope.row.dedAmonutUpperLimit" placeholder="请输入内容" style="width: 50px;" />
                元
              </el-col>
            </el-row>
            <el-row v-if="scope.$index !== 2" :gutter="20">
              <el-col :span="12" :offset="4">
                {{ getTitle(scope.row.dedTypeCode) }}>
                <el-input v-model="scope.row.timesLowerLimit" placeholder="请输入内容" style="width: 50px;" disabled />
                次，每次扣款
                <el-input v-model="scope.row.periodLowerLimit" placeholder="请输入内容" style="width: 50px;" />
                元
              </el-col>
            </el-row>
            <el-row v-if="scope.$index !== 2" :gutter="20">
              <el-col :span="12" :offset="2">
                {{ getTitle(scope.row.dedTypeCode) }}>
                <el-input v-model="scope.row.dedAmonutLowerLimit" placeholder="请输入内容" style="width: 50px;" disabled />
                分钟
              </el-col>
            </el-row>
            <el-row v-if="scope.$index !== 2" :gutter="20">
              <el-col :span="12" :offset="4">
                {{ getTitle(scope.row.dedTypeCode) }}>
                <el-input v-model="scope.row.absenceTimesUpperLimt" placeholder="请输入内容" style="width: 50px;" disabled />
                次，每次扣款
                <el-input v-model="scope.row.absenceDays" placeholder="请输入内容" style="width: 50px;" />
                元
              </el-col>
            </el-row>
          </div>
        </template>

      </el-table-column>
    </el-table>
    <div class="footer">

      <el-button type="primary" @click="submitForm">批量更新</el-button>

      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getDedList, saveCfgDeduction } from '@/api/attendance'
import { Message } from 'element-ui'
export default {
  inject: ['dlist'],
  data() {
    return {
      titles: [{ name: '迟到', code: 51000 }, { name: '早退', code: 52000 }, { name: '旷工', code: 53000 }],
      ruleForm: {
        id: 0,
        companyId: 0,
        departmentId: 0
      },
      rules: {
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ]
      },
      companyList: []
    }
  },
  created() {
    this.ruleForm.departmentId = this.dlist.lists[0].id
    this.load()
  },
  methods: {
    getTitle(code) {
      const nameList = this.titles.filter(item => String(item.code) === String(code))
      // console.log('test', nameList, code)
      if (nameList.length === 0) {
        return ''
      }
      return nameList[0].name
    },
    async load(id) {
      if (id) {
        this.ruleForm.departmentId = id
      }
      const res = await getDedList({ departmentId: this.ruleForm.departmentId })
      console.log('扣款', res)
      this.companyList = res
      this.companyList.forEach(item => {
        item.name = this.getTitle(item.code) + '扣款'
      })
      // const res = await getExtDutyItem({ departmentId: this.ruleForm.departmentId })
      // console.log('qingjiaLoadding - load', res)
    },
    changDept(id) {
      this.ruleForm.departmentId = id
      //   console.log(id)
      this.load()
    },
    closeDialog() {
      this.$emit('closeDialog')
    },
    async submitForm() {
      console.log('submit!', this.companyList)
      await saveCfgDeduction(this.companyList)
      Message.success('更新成功')
      this.closeDialog()
    }
  }
}
</script>

<style>
  .el-row {
    margin-bottom: 20px;
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  .footer{
        width: 100%;
        height: 100px;
        text-align: center;
        margin-top: 10px;
    }
</style>
